<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" size="small">
          <el-form-item label="服务名称">
            <el-input v-model="dataForm.serviceName" style="width: 215px" placeholder="服务名称"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
                v-model="dataForm.useTimeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="getDataList()">查询</el-button>
            <el-button type="primary" @click="createService">创建服务</el-button>
          </el-form-item>
        </el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%;">
          <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
          </el-table-column>
          <el-table-column
              prop="serName"
              header-align="center"
              align="center"
              label="服务名称">
          </el-table-column>
          <el-table-column
              prop="itemId"
              header-align="center"
              align="center"
              label="服务编号">
          </el-table-column>
          <el-table-column
              prop="serStatus"
              header-align="center"
              align="center"
              label="开启/关闭">
            <template #default="scope">
              <el-switch
                  @change="updateState(scope.row)"
                  v-model="scope.row.serStatus"
                  active-value="1"
                  inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
              prop="publishMode"
              header-align="center"
              align="center"
              label="开放限制"
              :formatter="showServLimit">
          </el-table-column>
          <el-table-column
              prop="servKeyword"
              header-align="center"
              align="center"
              label="关键词">
          </el-table-column>
          <el-table-column
              prop="createdTime"
              header-align="center"
              align="center"
              label="发布时间">
          </el-table-column>
          <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作">
            <template #default="scope">
              <el-button type="primary" size="mini" @click="updateService(scope.row.servId)">编辑</el-button>
              <el-button type="primary" size="mini" @click="deleteService(scope.row.servId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <service-updated v-if="serviceUpdatedVisible" ref="serviceUpdated" @refreshDataList="getDataList"></service-updated>
  </div>
</template>

<script>

import ServiceManageUpdateSeting from '../service-manage/ServiceManageUpdateSeting'
import ServiceManagePublishSeting from '../service-manage/ServiceManagePublishSeting'
import ServiceManagePreview from '../service-manage/ServiceManagePreview'
import {queryServiceList, deleteService, updateServiceState, queryServiceListByCondition} from "@/api/data_center/application-service";
import $axios from '@/network'
import ServiceUpdated from './ServiceUpdated'

/**
 *{ 服务管理=列表 }
 * @param:
 * @author: 宋康
 * @DateTime: 2022/7/2
 */
export default {
  name: "DataServiceList",
  components: {
    ServiceManagePublishSeting,
    ServiceManageUpdateSeting,
    ServiceManagePreview,
    ServiceUpdated
  },
  data() {
    return {
      dataForm: {
        serviceName: '',
        useTimeRange: []
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      serviceUpdatedVisible: false
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    dateFormat(date) {
      let time = date.toString().substring(0,19).replace("T"," ");
      return time;
    },
    getDataList() {
      if(!this.dataForm.useTimeRange){
        this.dataForm.useTimeRange=[]
      }
      this.dataListLoading = true
      let op = {
        'serviceName': this.dataForm.serviceName,
        'startTime': this.dataForm.useTimeRange[0],
        'endTime': this.dataForm.useTimeRange[1]
      }
      $axios({
        url:'/data/dataService/query/page',
        method: 'POST',
        params: {
          // 'total': this.totalPage,
          'current': this.pageIndex,
          'size': this.pageSize
        },
        data: {
          ...op
        },
        // headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
      }).then(data=>{
        if (data && data.code === 200) {
          this.dataList = data.data.records
          this.totalPage = data.data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    createService() {
      this.$router.push({
        name: 'DataServiceCreate'
      })
    },
    updateService(servId) {
      this.serviceUpdatedVisible = true
      this.$nextTick(() => {
        this.$refs.serviceUpdated.init(servId)
      })
    },
    deleteService(servId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteService(servId).then(res=>{
          if(res.status){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            // 刷新页面
            this.$router.go(0)
          }else {
            this.$message.error('删除失败');
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    updateState(serv){
      updateServiceState(serv.servId, serv.serStatus).then(res=>{
        if(res.status){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        }else {
          this.$message.error('操作失败');
        }
      })
    },
    showServLimit(row, column, cellValue, index){
      if(cellValue==1){
        return '可见'
      }
      if(cellValue==2){
        return '条件可见'
      }
      if(cellValue==3){
        return '线下'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
</style>
